package com.suneast.suneaststore.modules.launcher;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.suneast.suneaststore.MainActivity;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.ui.fragments.BaseFragment;
import com.suneast.suneaststore.util.storage.LattePreference;
import com.zhengsr.viewpagerlib.bean.PageBean;
import com.zhengsr.viewpagerlib.callback.PageHelperListener;
import com.zhengsr.viewpagerlib.indicator.TransIndicator;
import com.zhengsr.viewpagerlib.view.GlideViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LauncherScrollFragment extends BaseFragment {

    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_scroll_laucher;
    }

    @BindView(R.id.splash_viewpager)
    GlideViewPager viewPager;

    @BindView(R.id.splash_indicator)
    TransIndicator indicator;

    @BindView(R.id.btn_start_main)
    Button startMainBtn;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        //先把本地的图片 id 装进 list 容器中
        List<Integer> imgIdList = new ArrayList<>();
        imgIdList.add(R.drawable.guide01);
        imgIdList.add(R.drawable.guide02);
        imgIdList.add(R.drawable.guide03);

        //配置pagerbean，这里主要是为了viewpager的指示器的作用，然后把最后一页的button也添加进来，注意记得写上泛型
        PageBean bean = new PageBean.Builder<Integer>()
                .setDataObjects(imgIdList)
                .setIndicator(indicator)
                .setOpenView(startMainBtn)
                .builder();

        //把数据添加到 viewpager中，并把view提供出来，这样除了方便调试，也不会出现一个view，多个
        //parent的问题，这里在轮播图比较明显
        viewPager.setPageListener(bean, R.layout.layout_splash_image, new PageHelperListener() {
            @Override
            public void getItemView(View view, Object data) {
                //通过获取到这个view，你可以随意定制你的内容
                ImageView imageView = view.findViewById(R.id.icon);
                imageView.setImageResource((Integer) data);
            }
        });
    }

    @OnClick(R.id.btn_start_main)
    public void onViewClick(View v) {
        LattePreference.setAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name(), true);
        FragmentActivity activity = getActivity();
        activity.startActivity(new Intent(activity, MainActivity.class));
        activity.finish();
    }
}
